<template>
  <div class="stockCarriedForward">
    <el-table-form ref="form" :model="query">
      <el-form-item label="仓库名称:" prop="storeId">
        <el-select v-model="query.storeId" placeholder="请选择">
          <!-- <el-option label="请选择" :value="0" /> -->
          <el-option
            v-for="item in storeMap"
            :key="item.storeId"
            :label="item.storeName"
            :value="item.storeId"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="结转时间:" :table-form-merge="2" class="merge-item">
        <el-date-picker
          v-model="query.date"
          type="daterange"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>

      <template v-slot:btns>
        <el-button
          v-permission="1"
          type="primary"
          icon="el-icon-search"
          @click="search"
        >查询</el-button>
      </template>
    </el-table-form>
    <div style="margin-bottom: 10px;" class="btn-group">
      <div>
        <el-button
          v-permission="2"
          type="primary"
          icon="el-icon-plus"
          @click="carry"
        >结转</el-button>
      </div>
    </div>
    <lb-table
      ref="lbTable"
      v-el-height-adaptive-table="{ bottomOffset: 10 }"
      border
      highlight-current-row
      element-loading-text="加载中"
      :column="column"
      :data="tableData.data"
      pagination
      :total="tableData.total"
      custom-column
      @fetch="fetchData"
    />
    <AddDialog ref="AddDialogRef" :store-map="storeMap" @refresh="search" />
  </div>
</template>

<script>
  import { selectOrgSNNoAndReType, stockCarriedForwardList } from '@/api'
  import dayjs from 'dayjs'
  import { mapGetters } from 'vuex'
  import AddDialog from './components/AddDialog'
  export default {
    name: 'stockCarriedForward',
    components: {
      AddDialog
    },
    data() {
      return {
        storeMap: [],
        actionColWidth: 80,
        query: {
          storeId: '',
          date: []
        },
        tableData: {
          total: 0,
          data: []
        }
      }
    },
    computed: {
      ...mapGetters(['corpSettingData']),
      flag() {
        const sp2 = this.corpSettingData.sp2
        return (sp2 >> 12) & 0x01
      },
      column() {
        return [
          {
            type: 'index'
          },
          // {
          //   label: "结转单号",
          //   prop: "carryOverCode",
          //   width: 200,
          //   align: "center",
          //   // formatter: this.formatter.fontFormat,
          // },
          {
            label: '仓库名称',
            prop: 'storeName',
            align: 'center'
          // formatter: this.formatter.fontFormat,
          },
          {
            label: '结转时间',
            prop: 'happendTime',
            width: 200,
            align: 'center',
            // formatter: this.formatter.dateTimeFormat,
            render: (h, { row }) => {
              return (
              <div>
                <span>{dayjs(row.happendTime).format('YYYY-MM-DD hh:mm:ss')}</span>
              </div>
              )
            }
          },
          {
            label: '操作人',
            prop: 'createUser',
            width: 150,
            align: 'center'
          // // formatter: this.formatter.fontFormat,
          },
          {
            label: '操作时间',
            prop: 'createTime',
            width: 200,
            align: 'center',
            // // formatter: this.formatter.dateTimeFormat,
            render: (h, { row }) => {
              return (
              <div>
                <span>{dayjs(row.createTime).format('YYYY-MM-DD hh:mm:ss')}</span>
              </div>
              )
            }
          },
          {
            label: '状态',
            prop: 'status',
            width: 130,
            align: 'center',
            render: (h, { row }) => {
              return (
              <div>
                <span style={row.status === -1 && 'color:red'}>
                  {this.statusFormat(row.status)}
                </span>
              </div>
              )
            }
          // // formatter: this.formatter.statusFormat,
          },
          {
            label: '操作',
            align: 'center',
            fixed: 'right',
            width: 180,
            render: (h, { row, $index }) => {
              return (
              <div>
                <el-link v-permission='2' size='mini' onClick={() => this.detail(row)}>
                  明细
                </el-link>

                {!this.falg && (
                  <el-link v-permission='3' size='mini' onClick={() => this.edit(row)}>
                    编辑
                  </el-link>
                )}
                {row.status !== -1 && !this.falg && (
                  <el-link v-permission='3' size='mini' onClick={() => this.invalid(row)}>
                    作废
                  </el-link>
                )}
                {row.status !== 1 && !this.falg && (
                  <el-link v-permission='3' size='mini' onClick={() => this.del(row)}>
                    删除
                  </el-link>
                )}
                {row.status !== 1 && !this.falg && (
                  <el-link v-permission='3' size='mini' onClick={() => this.confirm(row)}>
                    确认
                  </el-link>
                )}
                {row.status !== 2 && !this.falg && (
                  <el-link v-permission='3' size='mini' onClick={() => this.revert(row)}>
                    反结转
                  </el-link>
                )}
              </div>
              )
            }
          }
        ]
      }
    },
    created() {
      this.initData()
    },
    methods: {
      // 自适应表格列宽
      flexColumnWidth() {
        this.$nextTick(() => {
          let max = 0
          const actionColDom = document.getElementsByClassName('action-col')
          for (let i = 0; i < actionColDom.length; i++) {
            if (actionColDom[i].tagName === 'TD') {
              let width = 20
              const linkDom = actionColDom[i].childNodes[0].childNodes[0].childNodes
              linkDom.forEach((it) => {
                width += it.offsetWidth + 10
              })
              if (width > max) max = width
            }
          }
          this.actionColWidth = max > 60 ? max - 10 : 50
        })
      },
      statusFormat(val) {
        switch (val) {
        case 1:
          return '待确认'
        case 2:
          return this.flag ? '已结转' : '已确认'
        case -1:
          return '作废'
        default:
          return ''
        }
      },
      _getQuery(obj) {
        const query = {}
        for (const key in obj) {
          if (obj.hasOwnProperty(key)) {
            if (obj[key] !== '') {
              query[key] = obj[key]
            }
          }
        }
        return query
      },
      async initData() {
        const { status, data } = await selectOrgSNNoAndReType({ reType: '4' })
        if (status === 200) {
          this.storeMap = data
        }
      },
      async fetchData({ currentPage, pageSize }) {
        const query = {
          ...this._getQuery(this.query),
          currentPage,
          pageSize
        }
        const { status, data } = await stockCarriedForwardList(query)
        if (status === 200) {
          this.tableData.data = data.rows
          if (currentPage === 1) this.tableData.total = data.total
        }
      },
      search() {
        this.$refs.lbTable.fetch({ currentPage: 1 })
      },
      // 结转
      carry() {
        this.$refs['AddDialogRef'].show()
      },
      detail(row) {},
      edit(row) {},
      revert(row) {},
      confirm(row) {},
      del(row) {},
      invalid(row) {}
    }
  }
</script>

<style lang="scss" scoped></style>
